<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="opendialog">增加停车计费规则</el-button>
      <el-button @click="exportToExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column label="序号" type="index" :index="indexMethod" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上限(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式" prop="chargeType">
          <template #default="{row}">
            {{ FormatTer(row.chargeType) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="editrules(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="deleterules(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination" style="float: right; margin-top: 20px;">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="getruleobj.pageSize"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <AddRule ref="viewrules" :dialog-visible.sync="dialogVisible" @getrulelist="getrulelist" />
  </div>
</template>

<script>
import { getrulelistAPI, deleterulesAPI } from '@/api/CarRule'
import { utils, writeFileXLSX } from 'xlsx'
import AddRule from './components/AddRule.vue'
export default {
  name: 'Building',
  components: {
    AddRule
  },
  data() {
    return {
      getruleobj: {
        page: 1,
        pageSize: 3
      },
      ruleList: [],
      total: null,
      dialogVisible: false
    }
  },

  created() {
    this.getrulelist()
  },
  methods: {
    // 获取计费列表
    async getrulelist() {
      const res = await getrulelistAPI(this.getruleobj)
      this.ruleList = res.data.rows
      // console.log(res)
      this.total = res.data.total
    },
    // 编辑计费规则
    editrules(id) {
      // console.log(id)
      // console.log(123213)
      this.dialogVisible = true
      this.$refs.viewrules.viewrules(id)
      this.$refs.viewrules.title = '编辑规则'
    },
    // 删除计划规则
    deleterules(id) {
      this.$confirm('你确定要删除吗', '温馨提示').then(async() => {
        await deleterulesAPI(id)
        this.$message.success('删除规则成功')
        if (this.ruleList.length === 1 && this.getruleobj.page > 1) {
          this.getruleobj.page--
        }
        this.getrulelist()
      }).catch(() => {})
    },
    // 增加计费规则打开模态框
    opendialog() {
      this.dialogVisible = true
      this.$refs.viewrules.title = '新增规则'
    },
    // 更改页码
    handleCurrentChange(val) {
      this.getruleobj.page = val
      this.getrulelist()
    },
    // 页码序号处理
    indexMethod(index) {
      // console.log(index)
      const res = (this.getruleobj.page - 1) * this.getruleobj.pageSize + index + 1
      // console.log(res)
      return res
    },
    // 格式化字段
    FormatTer(val) {
      // console.log(val)
      const TYPEMAP = {
        'duration': '按时长收费',
        'turn': '按次收费',
        'partition': '分段收费'
      }
      return TYPEMAP[val]
    },
    // 数据excel导出
    async exportToExcel() {
      // console.log(123)
      // 获取数据
      const res = await getrulelistAPI(this.getruleobj)
      // console.log(res.data.rows)
      // 定义导出的数据
      const tableHeaderKeys = ['ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView'] // 创建一个新的工作簿
      // 定义导出数据的表头
      const tableHeaderValues = ['计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上线(元)', '计费方式', '计费规则']
      // 导出新数组，按照要求导数据
      const list = res.data.rows.map(item => {
        const obj = {}
        tableHeaderKeys.forEach(ele => {
          if (ele === 'chargeType') {
            obj[ele] = this.FormatTer(item[ele])
          } else {
            obj[ele] = item[ele]
          }

          // obj[ele] = 123
        })
        return obj
        // console.log(obj)
      })
      // console.log(list)

      // 创建一个新的工作簿
      const workbook = utils.book_new()
      // 创建一个工作表 要求一个对象数组格式
      const worksheet = utils.json_to_sheet(list)

      // 把工作表添加到工作簿  Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, 'Data')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      // 导出方法进行导出
      writeFileXLSX(workbook, 'SheetJSVueAoO.xlsx')
    }
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
</style>
